$(function() {
	var userGrid = $("#payRecordGrid");
	
	userGrid.datagrid({
		url: 'payrecord/list',
		fit: true,
		pagination: true,
		border: false,
		fitColumns:true,
		onLoadSuccess:function(data){  
		    $(".note").tooltip({
		    	 onShow: function(){
		    		 $(this).tooltip('tip').css({
		                 width:'230',
	                     boxShadow: '1px 1px 3px #292929'
		    		 });
		    	 }
		    });  
		}, 
		columns: [
			[{
				field: 'checkbox',
				width: 100,
				align: 'center',
				checkbox: true
			}, {
				field: 'id',
				width: 20,
				align: 'center',
				hidden: 'true'
			
			}, {
				field: 'student',
				title: '学生姓名',
				align: 'center',
				width: 100,
				formatter: function (value, rec) {
			         return rec.student.name;
			     }
			}, {
				field: 'allMoney',
				title: '总金额',
				align: 'center',
				width: 100
			}, {
				field: 'yingJiao',
				title: '应交',
				align: 'center',
				width: 100,
				formatter: function (value,rec) {
					return '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#6699ff">'+((rec.allMoney)-(rec.discountMoney))+'</div>';
			     }
			}, {
				field: 'shiJiao',
				title: '实交',
				align: 'center',
				width: 100,
				formatter: function (value,rec) {
					if(rec.finish=="完成"){
						return '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#66cc66">'+((rec.allMoney)-(rec.discountMoney))+'</div>';
					}else{
						if(rec.shiJiao==null){
							return '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#66cc66">0</div>';
						}else{
							return  '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#66cc66">'+rec.shiJiao+'</div>';
						}
				     }
				}
			}, {
				field: 'qianJiao',
				title: '欠交',
				align: 'center',
				width: 100,
				formatter: function (value,rec) {
					if(rec.finish=="完成"){
						return '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#ff6666">0</div>';
					}else{
						return '<div style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#ff6666">'+(rec.allMoney-rec.discountMoney-rec.shiJiao)+'</div>';
					}
				}
			}, {
				field: 'discountMoney',
				title: '优惠金额',
				align: 'center',
				width: 90,
				formatter : function(value,rec){
					if(rec.discountMoney==null){
						return "无";
					}else{
						return rec.discountMoney;
					}
				}
			}, {
				field: 'discountReason',
				title: '优惠原因',
				align: 'center',
				width: 70,
				formatter: function(value, row, index) {
	                var abValue = value;
	                if (value.length>=22) {
	                    abValue = value.substring(0,19) + "...";
	                }
	                var content = '<a href="#" title="' + value + '" class="note">' + abValue + '</a>';
	                return content;
	            }
			}, {
				field: 'identity',
				title: '身份',
				align: 'center',
				width: 100
			/*}, {
				field: 'classes',
				title: '班级',
				align: 'center',
				width: 90,
				formatter: function (value, rec) {
			         return rec.classes.className;
			     }*/
			}, {
				field: 'banzhuren',
				title: '班主任',
				align: 'center',
				width: 100,
				formatter: function (value, rec) {
			         return rec.classes.classTeacherEmp['name'];
					
			     }
			}, {
				field: 'finish',
				title: '完成',
				align: 'center',
				width: 50,
				formatter : function(value,rec){
						if(rec.finish=='完成'){
							return '<img src="public/easyui/themes/icons/ok.png">';
						}else if(rec.finish=='未完成'){
							return '<img src="public/easyui/themes/icons/no.png">';
						}
					}
			
			}, {
				field: 'ac',
				title: '操作',
				width: 150,
				align: 'center',
				formatter: function(value, row, index) {
					var bts = [];	
					bts.push('<a data-index="' + index + '" class="ctr edit" >编辑</a>');
					bts.push('<a data-index="' + index + '" class="ctr cui">催款</a>');
					bts.push('<a data-index="' + index + '" class="ctr detail">详情</a>');
					return bts.join('');
				}
			}]
		],
		toolbar: [{
				iconCls: 'icon-add',
				text: "录入收款记录",
				url: "payrecord/add_payrecord",
				handler: function() {
					
					userForm();
				}
			},
			{
				iconCls: 'icon-redo',
				text: "导出",
				url: "",
				handler: function() {
					
					//获取复选框的选中个数
					var selRows = $('#payRecordGrid').datagrid('getSelections');
					//判断复选框是否选中
					if(selRows.length > 0){
						$.messager.confirm("提示", "确定将收款信息导入到学生信息表中？", function(r) {
							 if(r){
								//获取所选复选框中的值
								var checkedItems = $('#payRecordGrid').datagrid('getChecked');
								var names = [];
								   $.each(checkedItems, function(index, item){
									   //获取选中学员的ID
								       names.push(item.id);
								   });
								  
								   $.post("hight/daoChu" + (names ? ("/" + names) : ""), function(rs) {
									  		
									});
								 
								var win = $.messager.progress({
									title: 'Please waiting',
									msg: '导出中'
								});
								setTimeout(function() {
									$.messager.progress('close');
									$.messager.confirm("提示", "导出成功!", function(r) {
										var tabs=$("#tabs");
										var content = '<div scrolling="auto" frameborder="0"  style="width:100%;height:100%;"></div>';     
											  tabs.tabs('add',{    
									                              title:'学费信息',    
									                              content:content,    
									                              closable:true,
									                              href:'tuition'
									                            });   
									});
									
								}, 3200)
							 }else{
								//取消导出，关闭窗口
								 $.messager.alert("提示","亲，您还没有选择任何收款记录哦！");
							 }
							 
						});
					}else{
						$.messager.alert("提示","亲，您还没有选择任何收款记录哦！");
					}						
				}		
				
			}]
});

	//详情
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click", "a.detail", function() {
		var tab = $('#tabs').tabs('getSelected'); // 获取选择的面板
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = userGrid.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		var name=rows[index].student.name;
		//detailForm(id);
		

		$('#tabs').tabs('update', {
			tab: tab,
			options: {
				title: '收款管理',
				href: 'payrecord/payrecord_detail'+ (id ? ("/" + id) : "")+(name ? ("/" + name) : "")// 新内容的URL
			}
		});
		tab.panel('refresh');
	
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要删除此数据吗？", function(r) {
			if(r) {
				$.get("payrecord/delete/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						userGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("click", "a.edit", function() {
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = userGrid.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		userForm(id);
	}).on("click", "a.cui", function() {
		
		var index = this.dataset.index;
		var rows = userGrid.datagrid("getRows");
		
		var finish=rows[index].finish;
		var yj=rows[index].shiJiao;
		var s=rows[index].allMoney;
		var j=rows[index].discountMoney;
		var yingj = s-j;
		if(finish=='完成'){
			$.messager.alert("提示", "亲，该学生已完成缴费了，不能催缴哦！");
		}else if(yingj == yj){
			$.messager.alert("提示", "亲，该学生已完成缴费了，不能催缴哦！");
		}else{

			//获取收款id
			var shouKuanid = rows[index].id;
			//获取班主任id
			var teacherid=rows[index].classes.classTeacherEmp['id'];
			var ids=[];
			ids[0]=shouKuanid;
			ids[1]=teacherid;
			$.post("payrecord/cuikuan_save/" + ids, function(rs) {
				$.messager.alert("提示", "亲，消息已发送，请耐心等待处理哦！");
				
			});
		}
	});
	
	
	var identity=$("#identity");
	var finish=$("#finish");
	var student = $("#student");
	//检索按钮事件
	$("#payRecordSearch").on("click", function() {
		userGrid.datagrid("load", {
			student:student.val(),
			identity:identity.val(),
			finish: finish.val() 
	
		});
	});
	var form = $("#payRecordSearchForm");
	//重置按钮事件
	$("#payRecordReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});

	function userForm(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href: "payrecord/add_payrecord"+ (id ? ("/" + id) : ""),
			title: "录入收款信息",
			height: 370,
			width: 850,
			modal: true,
			onClose: function() {
				$(this).dialog("destroy");
			},
			buttons: [{
				iconCls: 'icon-save',
				text: '保存',
				handler: function() {
					var payRecordForm = $("#payRecordForm");
					if(payRecordForm.form('validate')) {
						$.post("payrecord/save", payRecordForm.serialize(), function(rs) {
							if(rs.success) {
								//刷新数据表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			}]

		});
	}

	function detailForm(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href: 'payrecord/payrecord_detail'+ (id ? ("/" + id) : ""),
			title: "&nbsp;&nbsp;二喜的收款详情",
			height: 400,
			iconCls: 'icon-shoukuan',
			width: 800,
			modal: true,
			onClose: function() {
				$(this).dialog("destroy");
			}

		});
	}
});


$.extend($.fn.datagrid.methods, {  
    tooltip : function (jq, fields) {  
        return jq.each(function () {  
            var panel = $(this).datagrid('getPanel');  
            if (fields && typeof fields == 'object' && fields.sort) {  
                $.each(fields, function () {  
                    var field = this;  
                    bindEvent($('.datagrid-body td[field=' + field + '] .datagrid-cell', panel));  
                });  
            } else {  
                bindEvent($(".datagrid-body .datagrid-cell", panel));  
            }  
        });  
  
        function bindEvent(jqs) {  
            jqs.mouseover(function () {  
                var content = $(this).text();  
                if(content && content!=""){  
                    $(this).tooltip({  
                        content : content,  
                        trackMouse : true,  
                        deltaX: 15,  
                        deltaY: -5,  
                        onShow: function(){  
                            $(this).tooltip('tip').css({  
                                backgroundColor: '#f7f5d1',  
                                borderColor: 'black'  
                            });  
                        }  
                    }).tooltip('show');  
                }  
            });  
        }  
    }  
});  
